'use strict';

const Controller = require('egg').Controller;

class UserController extends Controller {
    async sendCode() {
        const { ctx, service } = this
        let { mobile } = ctx.request.body

        let code = ctx.helper.createCode()
        let params = {
            "RegionId": "cn-hangzhou",
            "PhoneNumbers": mobile,
            "SignName": "人人快讯",
            "TemplateCode": "SMS_178455384",
            "TemplateParam": "{\"code\":\"" + code + "\"}"
        }

        let getData = await service.user.user.sendCode(params)

        if (typeof (getData) == 'string') {
            ctx.helper.failure({ ctx, msg: getData })
        } else {

            await this.app.redis.set("code:" + mobile, code, 'Ex', 60);

            ctx.helper.success({ ctx, res: getData })
        }
    }
}

module.exports = UserController;
